package com.mybatis.util;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionUtil {
    public static SqlSession getSqlSession() {
        SqlSession sqlSession = null;
        try{
            // 加载配置文件
            InputStream resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
            // 通过配置文件拿到sqlSessionFactory
            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
            // 通过factory 拿到sqlSession 实例
            sqlSession = sessionFactory.openSession();
        }catch (IOException e){
            e.printStackTrace();
        }
        return sqlSession;
    }
}
